//
//  MHComicDetailHeader.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHComicDetailHeader.h"
#import "NSString+Additional.h"

@interface MHComicDetailHeader ()
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@property (weak, nonatomic) IBOutlet UILabel *bookName;
@property (weak, nonatomic) IBOutlet UILabel *bookAuthor;
@property (weak, nonatomic) IBOutlet UILabel *bookCategory;
@property (weak, nonatomic) IBOutlet UILabel *bookClickCount;
@property (weak, nonatomic) IBOutlet UILabel *bookLastUpdate;
@property (weak, nonatomic) IBOutlet UILabel *bookState;
@property (strong, nonatomic) UILabel *bookdesc;
@property (strong, nonatomic) UIButton *collectionButton;
@property (strong, nonatomic) UIButton *readBurron;
@property (strong, nonatomic) UIButton *moreButton;
@property (nonatomic, assign) CGRect descFrame;
@property (nonatomic, assign) CGRect collectionButtonFrame;
@property (nonatomic, assign) CGRect readBurronFrame;
@property (nonatomic, assign) CGRect moreButtonFrame;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;

@end

@implementation MHComicDetailHeader

//- (instancetype)initWithFrame:(CGRect)frame
//{
//    if (self = [super initWithFrame:frame]) {
//        UIView *view = [[[NSBundle mainBundle]loadNibNamed:@"ComicDetailHeaderView" owner:nil options:nil] lastObject];
//        [self addSubview:view];
//        self.headerHeight = CGRectGetMaxY(self.collectionButton.frame) + 20;
//    }
//    return self;
//}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.bookdesc == nil) {
        self.bookdesc = [[UILabel alloc]init];
        self.bookdesc.font = [UIFont systemFontOfSize:13];
        self.bookdesc.numberOfLines = 2;
        self.bookdesc.frame = CGRectMake(30, CGRectGetMaxY(self.descLabel.frame), Width - 60, 50);
        self.bookdesc.autoresizesSubviews = NO;
    [self addSubview:self.bookdesc];
    
    self.collectionButton = [[UIButton alloc]init];
    [self.collectionButton addTarget:self action:@selector(collectBook:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionButton setTitle:@"追番（收藏）" forState:UIControlStateNormal];
    [self.collectionButton setTitleColor:[UIColor colorWithRed:0.558 green:1.000 blue:0.603 alpha:1.000] forState:UIControlStateNormal];
    [self addSubview:self.collectionButton];
        CGFloat collectionW = 120;
        CGFloat collectionX = Width / 2 - 40 - collectionW;
        CGFloat collectionH = 30;
        CGFloat collectionY = CGRectGetMaxY(self.bookdesc.frame);
        self.collectionButton.frame = CGRectMake(collectionX, collectionY, collectionW, collectionH);
        self.collectionButton.autoresizesSubviews = NO;
    
    self.readBurron = [[UIButton alloc]init];
    [self.readBurron addTarget:self action:@selector(readBook:) forControlEvents:UIControlEventTouchUpInside];
    [self.readBurron setTitle:@"开始阅读" forState:UIControlStateNormal];
    [self.readBurron setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.readBurron.backgroundColor = [UIColor colorWithRed:0.558 green:1.000 blue:0.603 alpha:1.000];
    [self addSubview:self.readBurron];
        CGFloat readX = Width / 2 + 40 ;
        self.readBurron.frame = CGRectMake(readX, collectionY, collectionW, collectionH);
        self.readBurron.autoresizesSubviews = NO;
    
    
        self.moreButton = [[UIButton alloc]init];
        [self.moreButton addTarget:self action:@selector(pullButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.moreButton setTitle:@"↓" forState:UIControlStateNormal];
        [self.moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:self.moreButton];
        self.moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
        CGFloat moreW = 10;
        CGFloat moreX = CGRectGetMaxX(self.bookdesc.frame);
        CGFloat moreY = CGRectGetMaxY(self.bookdesc.frame) - moreW;
        self.moreButton.frame = CGRectMake(moreX, moreY, moreW, moreW);
        self.moreButton.autoresizesSubviews = NO;
        
        self.moreButtonFrame = self.moreButton.frame;
        self.readBurronFrame = self.readBurron.frame;
        self.descFrame = self.bookdesc.frame;
        self.collectionButtonFrame = self.collectionButton.frame;
        
        self.headerHeight = CGRectGetMaxY(self.collectionButton.frame) + 20;
        if (self.changeHeaderHeightBlock != nil) {
            self.changeHeaderHeightBlock(self.headerHeight);
        }
    }
}

- (void)setComicIntroduct:(MHComicIntroduct *)comicIntroduct
{
    _comicIntroduct = comicIntroduct;
    
    self.bookName.text = comicIntroduct.BookName;
    self.bookAuthor.text = [NSString stringWithFormat:@"%@  ＞",comicIntroduct.BookAuthor];
    self.bookCategory.text = comicIntroduct.CatalogName;
    self.bookClickCount.text = [NSString stringWithFormat:@"%ld", comicIntroduct.BookClickCount];
    self.bookdesc.text = comicIntroduct.BookDescription;
    self.bookLastUpdate.text = comicIntroduct.BookUpdateDate;
    self.bookState.text = comicIntroduct.BookStateName;
}

- (void)collectBook:(id)sender {
}
- (void)readBook:(id)sender {
}
- (IBAction)pullButton:(id)sender {
    
    UIButton *button = sender;
    
    if ([button.currentTitle isEqualToString:@"↓"]) {
        self.bookdesc.numberOfLines = 0;
        CGFloat bookDescH = [NSString textFrameWithFont:self.bookdesc.font String:self.comicIntroduct.BookDescription maxSize:CGSizeMake(self.bookdesc.frame.size.width, 0)].height;
        CGRect bookDescFrame = self.bookdesc.frame;
        bookDescFrame.size.height = bookDescH;
    
        CGRect collectionFrame = self.collectionButton.frame;
        collectionFrame.origin.y = CGRectGetMaxY(bookDescFrame) + 10;
    
        CGRect readFrame = self.readBurron.frame;
        readFrame.origin.y = CGRectGetMaxY(bookDescFrame) + 10;
    
        CGRect buttonFrame = button.frame;
        buttonFrame.origin.y = CGRectGetMaxY(bookDescFrame) - 10;
    
        
        [UIView animateWithDuration:0.25 animations:^{
            [self.bookdesc setFrame:bookDescFrame];
            [self.collectionButton setFrame:collectionFrame];
            [self.readBurron setFrame:readFrame];
            [button setFrame:buttonFrame];
            [button setTitle:@"↑" forState:UIControlStateNormal];
            self.headerHeight = CGRectGetMaxY(self.collectionButton.frame) + 20;
            if (self.changeHeaderHeightBlock != nil) {
                self.changeHeaderHeightBlock(self.headerHeight);
            }
        }];
    
        }
    else
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.bookdesc.numberOfLines = 2;
            self.bookdesc.frame = self.descFrame;
            self.collectionButton.frame = self.collectionButtonFrame;
            self.readBurron.frame = self.readBurronFrame;
            button.frame = self.moreButtonFrame;
            [button setTitle:@"↓" forState:UIControlStateNormal];
            self.headerHeight = CGRectGetMaxY(self.collectionButton.frame) + 20;
            if (self.changeHeaderHeightBlock != nil) {
                self.changeHeaderHeightBlock(self.headerHeight);
            }
        }];

    }

}
@end
